package com.example.testapplication.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.testapplication.R;
import com.example.testapplication.adapter.MyAdapter;
import com.example.testapplication.adapter.ShoppingFragmentAdapter;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.Result;
import com.example.testapplication.entity.User;
import com.example.testapplication.entity.response.UserMsgResponse;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class MyMsgFragment extends BaseFragment {

    private ArrayList<Fragment> fragments = new ArrayList<>();
    private final String[] mTitles = {"全部"};
    private ViewPager viewPager;
    private TextView userName;
    private TextView userEmail;


    public static MyMsgFragment newInstance() {
        MyMsgFragment fragment = new MyMsgFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_my_msg;
    }

    @Override
    protected void initView() {
        userName=mRootView.findViewById(R.id.user_name);
        userEmail=mRootView.findViewById(R.id.user_email);
        viewPager = mRootView.findViewById(R.id.fixedViewPagerMyMsg);
    }

    @Override
    protected void initData() {
        fragments.add(AddressFragment.newInstance());
        viewPager.setOffscreenPageLimit(fragments.size());
        viewPager.setAdapter(new MyAdapter(getFragmentManager(),fragments,mTitles));
        setMsg();
    }

    private void setMsg() {
        HashMap<String,Object> params = new HashMap<>();
        Api.config(ApiConfig.USER_MSG,params).getRequest(getActivity(), new CallBack() {
            @Override
            public void onSuccess(String result) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.v("---vnv",result);
                        Gson gson = new Gson();
                        UserMsgResponse res = gson.fromJson(result, UserMsgResponse.class);
                        if(res.getCode() == 1){
                            Log.v("nima-->",res.getData().toString());
                            User user = res.getData();
                            userName.setText(user.getUser());
                            userEmail.setText(user.getEmail());
                            showToastAsync("成功");
                        }else{
                            showToastAsync("失败");
                        }
                    }
                });

            }

            @Override
            public void onFailure(IOException e) {

            }
        });
    }
}